package icu.feigroup.domain.activity.service.trial.node;

import icu.feigroup.domain.activity.model.entity.MarketProductEntity;
import icu.feigroup.domain.activity.model.entity.TrialBalanceEntity;
import icu.feigroup.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import icu.feigroup.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import icu.feigroup.types.design.framwork.tree.StrategyHandler;
import icu.feigroup.types.enums.ResponseCode;
import icu.feigroup.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * projectName:icu.feigroup.domain.activity.service.trial.node
 *
 * @author 飞
 * @since 2025/2/19 11:40
 * description:开关节点
 */
@Slf4j
@Service
public class SwitchNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {
    @Resource
    private MarketNode marketNode;

    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        String userId = requestParameter.getUserId();
        // 判断是否降级
        if (repository.downgradeSwitch()) {
            log.info("拼团活动降级拦截 {}", userId);
            throw new AppException(ResponseCode.E003.getCode(), ResponseCode.E003.getInfo());
        }
        if (!repository.cutRange(userId)){
            log.info("拼团切量拦截 {}", userId);
            throw new AppException(ResponseCode.E004.getCode(), ResponseCode.E004.getInfo());
        }
        return router(requestParameter,dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return marketNode;
    }
}
